﻿using Application.StudentService;
using AutoMapper;
using Domain.Entity;
using Microsoft.AspNetCore.Mvc;

namespace UI7_2.Controllers
{
    public class StudentController : Controller
	{
		private readonly IStudentAppService studentApp;
		private readonly IMapper mapper;

		public StudentController(IStudentAppService studentApp,IMapper mapper) 
		{
			this.studentApp = studentApp;
			this.mapper = mapper;
		}

		public IActionResult Index()
		{
			return View();
		}

		public IActionResult SelectStudent()
		{
			List<Student> students =  studentApp.StudentSelectAsync();
			return Json(students);
		}

		[HttpPost]
		public async Task<IActionResult> Add(Student student)
		{
			int result = await studentApp.StudentAddAsync(student);
			if (result > 0)
			{
				return Content(result.ToString());
			}
			else
			{
				return null; ;
			}
		}

		public async Task<int> StudentDelete(int id)
		{
			return await studentApp.StudentDeleteAsync(id);
		}

		public async Task<int> StudentUpdate(StudentAddDto studentAddDto)
		{
			Student student = mapper.Map<Student>(studentAddDto);
			return await studentApp.StudentUpdateAsync(student);
		}
	}
}
